//
//  HRLoginViewController.m
//  haierapp
//
//  Created by zhang on 14-8-5.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRLoginViewController.h"
#import "HRUser.h"

#import "HRRegisterAndForgotPwdViewController.h"


@interface HRLoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginButton;


@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation HRLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIImage *bgImage=[[UIImage imageNamed:@"btn"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [_loginButton setBackgroundImage:bgImage forState:UIControlStateNormal];
    
    self.navigationItem.title = @"用户登录";
    [self.navController showBackButtonWith:self andAction:@selector(back)];

}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [[HRCommonData shareCommonData] setLoginCompleteBlock:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods

- (IBAction)userLoginAction:(id)sender {

    NSString *nameStr=_userName.text;
    NSString *passwordStr=_password.text;
    
    [self hideTextFieldKeyboard];
    
    double startTime=[[NSDate date] timeIntervalSince1970];//开始时间
    
    [self showLoadingHud:kLoading];
    
    [HRInterfaceManager loginWithUserName:nameStr withPassword:passwordStr completion:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideLoadingHud];
        
        if (isSucceed) {
            HRUser *userModel=(HRUser *)data;
            
            //将登录用户存放到userDefaults
            [[HRCommonData shareCommonData] loginUser:userModel];
            
            if ([HRCommonData shareCommonData].loginCompleteBlock) {
                [HRCommonData shareCommonData].loginCompleteBlock(YES,@"登录成功");
            }
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else{
            if ([message isEqualToString:NO_WIFI_ERROR_CODE]) {
                message=@"请联网，进行登录操作";
            }
            [self showTextHUD:message];
        }
        
        //用户登录耗时【海尔检测平台】
        double endTime=[[NSDate date] timeIntervalSince1970];//结束时间
        double duration = endTime-startTime;
        duration = duration * 1000;
        //[uAnalysisManager onAppLoginEvent:duration];
    }];
}
- (IBAction)hideKeyboard:(id)sender {
    [self hideTextFieldKeyboard];
}

-(void)hideTextFieldKeyboard{
    [_userName resignFirstResponder];
    [_password resignFirstResponder];
}

#pragma mark - UITextField Delegate Methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - Navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    HRRegisterAndForgotPwdViewController *viewController=(HRRegisterAndForgotPwdViewController *)segue.destinationViewController;
    if ([segue.identifier isEqualToString:@"register"]) {
        //注册页面
        viewController.flag=PAGE_FLAG_REGISTER;
    }else if([segue.identifier isEqualToString:@"forgotpwd"]){
        //找回密码页面
        viewController.flag=PAGE_FLAG_FORGETPWD;
    }
}

@end
